package com.hxit.huaxiamelongarden.controller;

import com.hxit.huaxiamelongarden.domin.po.ChatMessage;
import com.hxit.huaxiamelongarden.domin.po.Friend;
import com.hxit.huaxiamelongarden.domin.vo.UserVo;
import com.hxit.huaxiamelongarden.service.ChatHistoryService;
import com.hxit.huaxiamelongarden.service.ChatMessageService;
import com.hxit.huaxiamelongarden.service.FriendService;
import com.hxit.huaxiamelongarden.service.UserService;
import com.hxit.huaxiamelongarden.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName ChatController
 * @description 消息
 * @Author 12694
 * @Date 2025/1/30 11:53
 * @Version 1.0
 **/
@RestController
@Slf4j
@Tag(name = "消息")
@RequestMapping("/chat")
public class ChatController {
    @Autowired
    private FriendService friendService;
    @Autowired
    private UserService userService;
    @Autowired
    private ChatMessageService chatMessageService;
    @Autowired
    private ChatHistoryService chatHistoryService;

    //获取互相关注的列表
    @Operation(summary = "获取互相关注的列表")
    @GetMapping("/eachFriends")
    public Result eachFriends(@RequestAttribute("userId") String userId){
        List<Friend> eachOtherFriends = friendService.getEachOtherFriends(Integer.parseInt(userId));
        if(eachOtherFriends == null || eachOtherFriends.size() == 0){
            return Result.success();
        }
        List<UserVo> collect = eachOtherFriends.stream().map(friend -> {
            UserVo userVo = userService.selectById(friend.getUserId());
            return userVo;
        }).collect(Collectors.toList());
        return Result.success(collect);
    }
    //获取关注列表
    @Operation(summary = "获取关注列表")
    @GetMapping("/friends")
    public Result friends(@RequestAttribute("userId") String userId){
        List<Friend> friends = friendService.getMyFriends(Integer.parseInt(userId));
        if (friends==null){
            return Result.success();
        }
        List<UserVo> collect = friends.stream().map(friend -> {
            UserVo userVo = userService.selectById(friend.getFriendId());
            return userVo;
        }).collect(Collectors.toList());
        return Result.success(collect);
    }
    //获取粉丝列表
    @Operation(summary = "获取粉丝列表")
    @GetMapping("/fans")
    public Result eachOtherFriends(@RequestAttribute("userId") String userId){
        List<Friend> myFans = friendService.getMyFans(Integer.parseInt(userId));
        if (myFans==null){
            return Result.success();
        }
        List<UserVo> collect = myFans.stream().map(friend -> {
            UserVo userVo = userService.selectById(friend.getUserId());
            return userVo;
        }).collect(Collectors.toList());
        return Result.success(collect);
    }

    // 发送消息
    @Operation(summary = "发送消息")
    @PostMapping("/send")
    public Result<String> sendMessage(@RequestParam("senderId") int senderId,
                                      @RequestParam("receiverId") int receiverId,
                                      @RequestParam("message") String message) {
        try {
            // 保存聊天消息到数据库
            chatMessageService.saveMessage(senderId, receiverId, message);
            log.info("消息发送成功，发送者ID：{}，接收者ID：{}", senderId, receiverId); // 打印中文日志
            return Result.success("消息发送成功");
        } catch (Exception e) {
            log.error("消息发送失败，发送者ID：{}，接收者ID：{}", senderId, receiverId, e); // 打印中文日志和异常信息
            return Result.error("消息发送失败，请稍后再试");
        }
    }

    // 查询用户与好友的聊天记录
    @Operation(summary = "查询用户与好友的聊天记录")
    @GetMapping("/history")
    public Result<List<ChatMessage>> getChatHistory(@RequestParam("userId") int userId,
                                                    @RequestParam("friendId") int friendId) {
        try {
            // 查询聊天记录
            List<ChatMessage> chatHistory = chatHistoryService.getChatHistory(userId, friendId);
            log.info("成功查询聊天记录，用户ID：{}，好友ID：{}", userId, friendId); // 打印中文日志
            return Result.success(chatHistory);
        } catch (Exception e) {
            log.error("查询聊天记录失败，用户ID：{}，好友ID：{}", userId, friendId, e); // 打印中文日志和异常信息
            return Result.error("查询聊天记录失败，请稍后再试");
        }
    }

}
